package com.ujiuye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ujiuye.mapper.MenuMapper;
import com.ujiuye.pojo.Menu;
import com.ujiuye.service.IMenuService;
import com.ujiuye.vo.Label;
import com.ujiuye.vo.MenuVo;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单 服务实现类
 * </p>
 *
 * @author zs
 * @since 2021-07-05
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Resource
    private MenuMapper menuMapper;

    @Override
    public List<MenuVo> listMenu() {
       QueryWrapper<Menu> queryWrapper = new QueryWrapper();
       queryWrapper.isNull("parentMenuId");
        menuMapper.selectList(queryWrapper);
        List<Menu> menus = menuMapper.selectList(queryWrapper);
        List<MenuVo> menuVos = new ArrayList<>();
        if (menus!=null && menus.size()>0){
            for (Menu menu : menus) {
                QueryWrapper<Menu> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("parentMenuId",menu.getId());
                List<Menu> sonMenu = menuMapper.selectList(queryWrapper1);
                List<Label> labels = new ArrayList<>();
                for (Menu sonMenu1 : sonMenu) {
                   Label label = new Label(sonMenu1.getId(),sonMenu1.getName());
                    labels.add(label);
                }
                MenuVo menuVo = new MenuVo(menu.getId(),menu.getName(),labels);
                menuVos.add(menuVo);
            }
        }
        return menuVos;
    }



    @Override
    public List<Menu> getParent() {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper();
        queryWrapper.isNull("parentMenuId");
        List<Menu> menus = menuMapper.selectList(queryWrapper);
        return menus;
    }


}
